package mi.payment.controller;

import android.text.TextUtils;

import mi.payment.R;
import mi.payment.bean.db.TradeData;
import mi.payment.constants.Params;
import mi.payment.db.UserInfoModel;
import mi.payment.interfaces.ITaskListener;
import mi.payment.settings.SignOperation;
import mi.payment.ui.base.ParentActivity;
import mi.payment.utils.SystemDateTime;
import mi.payment.utils.transaction.CustomParamsInitTask;
import mi.payment.utils.transaction.ParamsInitTask;


/**
 * 通过控制签到前后的交易来初始化某些参数
 * 主密钥下载->参数下载->签到->上送终端状态->AID下载->CAPK下载->IC卡黑名单下载->非接参数下载->卡binA下载->卡binB下载
 *
 * @author tomcat on 2017/4/28.
 */
public class ParamsInitCtrl extends BaseController {

    public ParamsInitCtrl(ParentActivity activity) {
        super(activity);
    }

    public void paramsInit(final ITaskListener<String> listener) {
        if (Params.userInfo == null) {
            Params.userInfo = UserInfoModel.getInstance().queryUserInfoById("01");
        }
        TradeData tradeData = Params.newTradeData();
        ParamsInitTask task = new CustomParamsInitTask() {
            @Override
            protected void onPostExecuteImpl(String ret) {
                if (TextUtils.equals("00", ret)) {
                    // 保存签到时间
                    int time = SystemDateTime.getCurrentYYYYMMDD();
                    SignOperation.getInstance().saveLastSignTime(time);
                    ParamsInitTask.isChecked = true;
                    listener.onTaskSuccess(ret);
                } else {
                    listener.onTaskFailed(ret);
                }
            }

            @Override
            protected void onTaskTimeoutImpl() {
                listener.onTaskFailed("68");
            }
        };
        task.setLoadDialog(activity.getLoadingDialog(getString(R.string.auto_sign)));
        task.execute(tradeData);
    }

}
